<?php

include('../../Redirect_modules.php');


DrawBC("Billing > ".ProgramTitle());

Widgets('billing_activity');
Widgets('course');

$extra['SELECT'] = ",Concat(NULL) AS DONE_DATE";
$extra['functions'] = array('DONE_DATE'=>'_makeDoneDate');
$extra['columns_after'] = array('DONE_DATE'=>'</A>Complete Date<A>');
Search('student_id',$extra);
if($_REQUEST['modfunc']=='add' || $_REQUEST['student_id'])
{
    if($_REQUEST['student_id'])
		$RET = DBGet(DBQuery('SELECT FIRST_NAME,LAST_NAME,MIDDLE_NAME,NAME_SUFFIX FROM students WHERE STUDENT_ID=\''.$_REQUEST['student_id'].'\''));
	else
		$RET = DBGet(DBQuery('SELECT FIRST_NAME,LAST_NAME,MIDDLE_NAME,NAME_SUFFIX FROM students WHERE STUDENT_ID=\''.UserStudentID().'\''));
    $count_student_RET=DBGet(DBQuery('SELECT COUNT(*) AS NUM FROM students'));
    if($count_student_RET[1]['NUM']>1){
		DrawHeaderHome( 'Selected Student: '.$RET[1]['FIRST_NAME'].'&nbsp;'.($RET[1]['MIDDLE_NAME']?$RET[1]['MIDDLE_NAME'].' ':'').$RET[1]['LAST_NAME'].'&nbsp;'.$RET[1]['NAME_SUFFIX'].' (<A HREF=Side.php?student_id=new&modcat='.clean_param($_REQUEST['modcat'],PARAM_NOTAGS).'><font color=red>Deselect</font></A>) | <A HREF=Modules.php?modname='.clean_param($_REQUEST['modname'],PARAM_NOTAGS).'&search_modfunc=list&next_modname=Students/Student.php&ajax=true&bottom_back=true&return_session=true target=body>Back to Student List</A>');
    }
	else if($count_student_RET[1]['NUM']==1){
        DrawHeaderHome( 'Selected Student: '.$RET[1]['FIRST_NAME'].'&nbsp;'.($RET[1]['MIDDLE_NAME']?$RET[1]['MIDDLE_NAME'].' ':'').$RET[1]['LAST_NAME'].'&nbsp;'.$RET[1]['NAME_SUFFIX'].' (<A HREF=Side.php?student_id=new&modcat='.clean_param($_REQUEST['modcat'],PARAM_NOTAGS).'><font color=red>Deselect</font></A>) ');
    }
}
if($_REQUEST['modfunc']=='add' && AllowEdit())
{
	
	if($_REQUEST['new_activity']!= '')
	{
		$bExist = DBGet(DBQuery('SELECT * FROM student_fee_activities WHERE SYEAR=\''.UserSyear().'\' AND STUDENT_ID=\''.UserStudentID().'\' AND ACTIVITY_ID=\''.$_REQUEST['new_activity'].'\''));
		if(count($bExist)==0)
		{
			DBQuery('INSERT INTO student_fee_activities (STUDENT_ID,ACTIVITY_ID,SYEAR) values(\''.UserStudentID().'\',\''.$_REQUEST['new_activity'].'\',\''.UserSyear().'\')');
		}
	}
	else 
	{
		if(count($_REQUEST['month_fee']))
		{
			foreach($_REQUEST['month_fee'] as $fee=>$m)
			{
				$month = $m['DONE_DATE'];
				if($month == 'JAN')
					$month = '01';
				elseif($month == 'FEB')
					$month = '02';
				elseif($month == 'MAR')
					$month = '03';
				elseif($month == 'APR')
					$month = '04';
				elseif($month == 'MAY')
					$month = '05';
				elseif($month == 'JUN')
					$month = '06';
				elseif($month == 'JUL')
					$month = '07';
				elseif($month == 'AUG')
					$month = '08';
				elseif($month == 'SEP')
					$month = '09';
				elseif($month == 'OCT')
					$month = '10';
				elseif($month == 'NOV')
					$month = '11';
				elseif($month == 'DEC')
					$month = '12';
				
				$day = $_REQUEST['day_fee'][$fee]['DONE_DATE'];
				$year = $_REQUEST['year_fee'][$fee]['DONE_DATE'];
				$date = $year.'-'.$month.'-'.$day;
				$sql = 'UPDATE student_fee_activities SET DONE_DATE=\''.$date.'\' WHERE SYEAR=\''.UserSyear().'\' AND STUDENT_ID=\''.UserStudentID().'\' AND ACTIVITY_ID=\''.$fee.'\'';
				DBQuery($sql);
			}	
		}
	}
	unset($_REQUEST['modfunc']);
}

if($_REQUEST['modfunc']=='remove' && AllowEdit())
{
	if(DeletePrompt('activity'))
	{
		DBQuery('DELETE FROM student_fee_activities WHERE STUDENT_ID=\''.UserStudentID().'\' AND ACTIVITY_ID=\''.$_REQUEST['activity_id'].'\' AND SYEAR=\''.UserSyear().'\'');
		unset($_REQUEST['modfunc']);
	}
}

if(UserStudentID() && !$_REQUEST['modfunc'])
{
	$start_end_RET = DBGet(DBQuery('SELECT TITLE,VALUE FROM program_config WHERE SYEAR=\''.UserSyear().'\' AND SCHOOL_ID=\''.UserSchool().'\' AND PROGRAM=\'eligibility\' AND TITLE IN (\''.'START_DAY'.'\',\''.'END_DAY'.'\')'));
	if(count($start_end_RET))
	{
		foreach($start_end_RET as $value)
			$$value['TITLE'] = $value['VALUE'];
	}
	
	switch(date('D'))
	{
		case 'Mon':
		$today = 1;
		break;
		case 'Tue':
		$today = 2;
		break;
		case 'Wed':
		$today = 3;
		break;
		case 'Thu':
		$today = 4;
		break;
		case 'Fri':
		$today = 5;
		break;
		case 'Sat':
		$today = 6;
		break;
		case 'Sun':
		$today = 7;
		break;
	}
	
	$start = time() - ($today-$START_DAY)*60*60*24;
	$end = time();
	
	if(!$_REQUEST['start_date'])
	{
		$start_time = $start;
		$start_date = strtoupper(date('d-M-y',$start_time));
		$end_date = strtoupper(date('d-M-y',$end));
	}
	else
	{
		$start_time = $_REQUEST['start_date'];
		$start_date = strtoupper(date('d-M-y',$start_time));
		$end_date = strtoupper(date('d-M-y',$start_time+60*60*24*7));
	}

	$sql = 'SELECT min(unix_timestamp(SCHOOL_DATE)) as SCHOOL_DATE FROM attendance_calendar WHERE SCHOOL_ID=\''.UserSchool().'\' AND SYEAR=\''.UserSyear().'\'';
	$begin_year = DBGet(DBQuery($sql));
	$begin_year = $begin_year[1]['SCHOOL_DATE'];
	
	$date_select = "<OPTION value=$start>".date('M d, Y',$start).' - '.date('M d, Y',$end).'</OPTION>';
	
	if($begin_year != "" || !begin_year)
	{
	for($i=$start-(60*60*24*7);$i>=$begin_year;$i-=(60*60*24*7))
		$date_select .= "<OPTION value=$i".(($i+86400>=$start_time && $i-86400<=$start_time)?' SELECTED':'').">".date('M d, Y',$i).' - '.date('M d, Y',($i+1+(($END_DAY-$START_DAY))*60*60*24)).'</OPTION>';
	}
	
	echo "<FORM name=elig_stud id=elig_stud action=Modules.php?modname=$_REQUEST[modname] method=POST>";
	DrawHeaderHome('<SELECT name=start_date>'.$date_select.'</SELECT>','<INPUT type=submit value=Go class=btn_medium onclick=\'formload_ajax("elig_stud");\' >');
	echo '</FORM>';

	echo '<TABLE border=0 width=100%><TR><TD width=50% valign=top>';
	
	
	$activities_RET = DBGet(DBQuery('SELECT ID,TITLE FROM fee_activities WHERE SYEAR=\''.UserSyear().'\' AND SCHOOL_ID=\''.UserSchool().'\''));
	if(count($activities_RET))
	{
		foreach($activities_RET as $value)
			$activities[$value['ID']] = $value['TITLE'];
	}

	$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove&start_date=$_REQUEST[start_date]";
//	$link['remove']['link'] = "#"." onclick='check_content(\"ajax.php?modname=$_REQUEST[modname]&modfunc=remove&start_date=$_REQUEST[start_date]\");'";
	$link['remove']['variables'] = array('activity_id'=>'ACTIVITY_ID');
	$link['add']['html']['TITLE'] = '<TABLE border=0 cellpadding=0 cellspacing=0><TR><TD>'.SelectInput('','new_activity','',$activities).' </TD></TR></TABLE>';
	$link['add']['html']['remove'] = button('add');

	echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=add&start_date=$_REQUEST[start_date] method=POST>";
	
	$profile = DBGET(DBQuery('SELECT PROFILE_ID FROM staff WHERE STAFF_ID=\''.UserID().'\''));
	if($profile[1]['PROFILE_ID'] ==1 || $profile[1]['PROFILE_ID'] == 6)
	{
		$functions = array('DONE_DATE' =>'_makeEditDateDetail', 'START_DATE' =>'_makeDateDetail', 'END_DATE' =>'_makeDateDetail'); 
	}
	else 
	{
		$functions = array('DONE_DATE' =>'_makeDateDetail', 'START_DATE' =>'_makeDateDetail', 'END_DATE' =>'_makeDateDetail'); 
	}
	
	
	$RET = DBGet(DBQuery('SELECT em.STUDENT_ID,em.ACTIVITY_ID,ea.TITLE, ea.START_DATE, ea.END_DATE, em.DONE_DATE FROM fee_activities ea,student_fee_activities em WHERE em.SYEAR=\''.UserSyear().'\' AND em.STUDENT_ID=\''.UserStudentID().'\' AND em.SYEAR=ea.SYEAR AND em.ACTIVITY_ID=ea.ID ORDER BY ea.START_DATE'), $functions);
	
	$columns = array('TITLE'=>'Fee','START_DATE'=>'Starts','END_DATE'=>'Ends', 'DONE_DATE'=>'Complete Date');//
	
	ListOutput($RET,$columns,'Fee','Fees',$link);
	echo '<BR><CENTER>'.SubmitButton('Save','','class=btn_medium onclick=\'formload_ajax("elig_stud");\'').'</CENTER>';
	echo '</FORM>';

	echo '</TD>';
	
	echo '</TR></TABLE>';
}

function _makeChooseCheckboxEdit($value,$title)
{	global $THIS_RET;
	
	if($value == '1')
		return "<INPUT type=checkbox name=activity_id[".$THIS_RET['ACTIVITY_ID']."] value=Y checked>";
	else 
		return "<INPUT type=checkbox name=activity_id[".$THIS_RET['ACTIVITY_ID']."] value=Y>";
}

function _makeLower($word)
{
	return ucwords(strtolower($word));
}

function _makeDoneDate($value,$title)
{	global $THIS_RET;
	$RET = DBGet(DBQuery('SELECT em.DONE_DATE FROM fee_activities ea,student_fee_activities em WHERE em.SYEAR=\''.UserSyear().'\' AND em.STUDENT_ID=\''.$THIS_RET['STUDENT_ID'].'\' AND em.SYEAR=ea.SYEAR AND em.ACTIVITY_ID=ea.ID ORDER BY em.DONE_DATE DESC'));
	if(count($RET)!= 0)
		if($RET[1]['DONE_DATE'])
			return date('M/d/Y',strtotime($RET[1]['DONE_DATE']));
		else 
			return "";
	else 
		return "";
}

function _makeEditDateDetail($value,$title)
{	global $THIS_RET;
	if($THIS_RET[$title])
		return DateInput($THIS_RET[$title],'fee['.$THIS_RET['ACTIVITY_ID'].']['.$title.']','');
	else 
		return DateInput('','fee['.$THIS_RET['ACTIVITY_ID'].']['.$title.']','');
}

function _makeDateDetail($value,$title)
{	global $THIS_RET;
		if($THIS_RET[$title])
			return date('M/d/Y',strtotime($THIS_RET[$title]));
		else
			return "";
}
?>